const router = require('koa-router')()
router.prefix('/user') // 添加路由前缀
var Uesers = require("../../model/agk/user");
console.log(`进入路由-接口`)
router.get('/getUsers', async (ctx, next) => {
  console.log(`进入接口`)
  await Uesers.findAll().then((data) => {
    ctx.body = {
      code: 0,
      data: data
    }
  }).catch(err => {
    console.log(err)
    ctx.body = {
      msg: '获取失败',
      code: -1
    }
  })
})
router.post('/add', async (ctx, next) => {
  await Uesers.findOne({
    where: {
      phone: ctx.request.body.phone
    }
  }).then((r) => {
    if (r) {
      ctx.body = {
        code: -1,
        msg: '此手机号已存在'
      }
      return
    }
    return Uesers.create(ctx.request.body).then((res) => {
      ctx.body = {
        code: 0,
        msg: '报名成功'
      }
    }).catch(err => {
      ctx.body = {
        msg: '失败',
        code: -1
      }
    })
  })
})


/*社团活动*/
router.get('/getScore', async (ctx, next) => {
  await Tvs.findAll().then(data => {
    ctx.body = {
      code: 0,
      data: data[0]
    }
  }).catch(err => {
    ctx.body = {
      msg: '获取失败',
      code: -1
    }
  })
})
router.post('/setScore', async (ctx, next) => {
  if (ctx.request.body.red === '' && ctx.request.body.blue === '') {
    ctx.body = {
      msg: '参数不全',
      code: -1
    }
    return
  }
  let data = {}
  if (ctx.request.body.red !== '') {
    data.red = ctx.request.body.red
  }
  if (ctx.request.body.blue !== '') {
    data.blue = ctx.request.body.blue
  }
  await Tvs.update(data, {
    where: {
      id: 1
    }
  }).then(r => {
    ctx.body = {
      msg: '更新成功',
      code: 0
    }
  }).catch(err => {
    ctx.body = {
      msg: '更新失败',
      code: -1
    }
  })

})

module.exports = router
